package entity.text;

import entity.file.AbstractFile;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ：hzs
 * @date ：Created in 2020/9/16 23:05
 * @description：
 * @modified By：
 * @version: $
 */
@Deprecated
public class Paper implements Text{

    public int id;

    public String title;

    public String xmlFile;

    public String paperAbstract;

    public String keywords;

    public String authors;

    public String publish_year;

    public String paperText;

    public double lineLength;

    public List<Chapter> chapters;

    public Paper(AbstractFile abstractFile) {
        this.id = abstractFile.id;
        this.title = abstractFile.title;
        this.xmlFile = abstractFile.xmlFile;
        this.paperAbstract = abstractFile.paperAbstract;
        this.keywords = abstractFile.keywords;
        this.authors = abstractFile.authors;
        this.publish_year = abstractFile.publish_year;
        this.paperText = abstractFile.paperText;
        this.lineLength = abstractFile.lineLength;
        chapters = new ArrayList<>();
    }

    public void extractChapters() {
        String paperText = this.paperText.replace("\n?", "\n");
        paperText = removeAcknowledgementsAndSoOn(paperText);
//        准备工作做好了

//        扫描文本的指针
        int leftPointer = 0;
        int rightPointer = 0;
//        抽取结果放这里
        Chapter chapter = new Chapter();
        int firstNumber = 0;
        Matcher m1 = null;
//        准备好了，开始抽
        do {
//            如果不是第一次找标题
            if (firstNumber > 1) {
                rightPointer = m1.start();
                chapter.content = paperText.substring(leftPointer, rightPointer);
                this.chapters.add(chapter);
                chapter = new Chapter();
            }
//            如果不是第一次循环
            if (firstNumber != 0) {
//                假设现在firstNumber是2，可这里先处理1的标题
//            序号
                chapter.index = String.valueOf(firstNumber);
//            小标题
                leftPointer = m1.end();
                rightPointer = readLineAlong(paperText, leftPointer);
                chapter.subTitle = paperText.substring(leftPointer, rightPointer);
//            正文，可能为空，开始第二层
                leftPointer = rightPointer;
                Matcher m2 = null;
                int secondNumber = 0;
                String pattern2 = null;
                do {
//                    如果不是第一次找
                    if (secondNumber != 0) {
                        rightPointer = m2.start();
                        chapter.content = paperText.substring(leftPointer, rightPointer);
                        this.chapters.add(chapter);
                        chapter = new Chapter();
//                      序号
                        chapter.index = String.valueOf(firstNumber + "." + secondNumber);
//                      小标题
                        leftPointer = m2.end();
                        rightPointer = readLineAlong(paperText, leftPointer);
                        chapter.subTitle = paperText.substring(leftPointer, rightPointer);
//                      正文，可能为空，开始第三层
                        leftPointer = rightPointer;
                        Matcher m3 = null;
                        int thirdNumber = 0;
                        do {
//                            如果不是第一次找
                            if (thirdNumber != 0) {
                                rightPointer = m3.start();
                                try {
                                    chapter.content = paperText.substring(leftPointer, rightPointer);
                                } catch (Exception e) {
//                                    有时会出现content为空的情况，例如一个二级标题紧贴三级标题
                                    chapter.content = "";
                                }
                                this.chapters.add(chapter);
                                chapter = new Chapter();
//                      序号
                                chapter.index = String.valueOf(firstNumber + "." + secondNumber + "." + thirdNumber);
//                      小标题
                                leftPointer = m3.end();
                                rightPointer = readLineAlong(paperText, leftPointer);
                                chapter.subTitle = paperText.substring(leftPointer, rightPointer);
//                      正文，可能为空，开始第四层
                                leftPointer = rightPointer;
                                Matcher m4 = null;
                                int fourthNumber = 0;
                                do {
                                    if (fourthNumber != 0) {
                                        rightPointer = m4.start();
                                        chapter.content = paperText.substring(leftPointer, rightPointer);
                                        this.chapters.add(chapter);
                                        chapter = new Chapter();
//                      序号
                                        chapter.index = String.valueOf(firstNumber + "." + secondNumber
                                                + "." + thirdNumber + "." + fourthNumber);
//                      小标题
                                        leftPointer = m4.end();
                                        rightPointer = readLineAlong(paperText, leftPointer);
                                        chapter.subTitle = paperText.substring(leftPointer, rightPointer);
                                    }
                                    leftPointer = rightPointer;
                                    fourthNumber++;
                                    String pattern4 = "\n" + firstNumber + "\\." + secondNumber + "."
                                            + thirdNumber + "." + fourthNumber + "\\. ";
                                    m4 = Pattern.compile(pattern4).matcher(paperText);
                                } while (m4.find(leftPointer));
                            }
                            thirdNumber++;
                            String pattern3 = "\n" + firstNumber + "\\."
                                    + secondNumber + "\\." + thirdNumber + "\\. ";
                            m3 = Pattern.compile(pattern3).matcher(paperText);
                        } while (m3.find(leftPointer));
                    }
                    secondNumber++;
                    pattern2 = "\n" + firstNumber + "\\." + secondNumber + "\\. ";
                    m2 = Pattern.compile(pattern2).matcher(paperText);
                } while (m2.find(leftPointer));

            }
            firstNumber++;
            String pattern1 = "\n" + firstNumber + "\\. ";
            m1 = Pattern.compile(pattern1).matcher(paperText);
        } while (m1.find(leftPointer));
//        收尾
        chapter.content = paperText.substring(leftPointer);
        this.chapters.add(chapter);
    }

    public String removeAcknowledgementsAndSoOn(String input) {
        String result = input;
        Matcher matcher = Pattern.compile("\nAcknowledgement").matcher(result);
        if (matcher.find()) {
            result = result.substring(0, matcher.start());
        }
        matcher = Pattern.compile("\nAppendix").matcher(result);
        if (matcher.find()) {
            result = result.substring(0, matcher.start());
        }
        matcher = Pattern.compile("\nReference").matcher(result);
        if (matcher.find()) {
            result = result.substring(0, matcher.start());
        }
        return result;
    }

    public int readLineAlong(String inputText, int startIndex) {
        int end = startIndex;
        if (end != inputText.length() && inputText.charAt(end+1) == '\n') {
            end++;
        }
        do {
            end++;
        } while (end < inputText.length() && inputText.charAt(end) != '\n');
        if (end >= inputText.length()) {
            return inputText.length();
        }
        return end+1;
    }
}
